// pages/food/details.js
const util = require('../../utils/util')
const app = getApp();
import {
  billFareFind,
  billFareAffiliated,
  billFareList
} from '../../api/billFare'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    currentIndex: 1,
    videoHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let sysinfo = wx.getSystemInfoSync()
    let navHeight=44
    if (!(sysinfo.system.indexOf('iOS') > -1)) {
      navHeight = 48;
    } 
    this.setData({
      videoHeight: sysinfo.screenHeight - sysinfo.statusBarHeight - navHeight - 20
    })
    billFareFind(options.id).then(e => {
      let _billFare = e.data.billFare
      _billFare.billFareMeta.images = _billFare.billFareMeta.images.map(e => util.thumbnail(e, 710, 430))
      this.setData({
        billFare: _billFare
      })
    })
    billFareAffiliated({
      billFareId: options.id,
      pageSize: 4
    }).then(e => {
      e.data.data.forEach(item => {
        item.logo = util.thumbnail(item.logo, 156, 156)
      })
      let _billFareAffiliated = e.data.data
      this.setData({
        billFareAffiliated: _billFareAffiliated
      })
    })
    billFareList({
      pageSize: 3
    }).then(e => {
      let _billFareList = e.data.data
      let delIndex = -1
      _billFareList = _billFareList.map((e, i) => {
        if (options.id == e.id)
          delIndex = i
        return {
          ...e,
          fromNow: util.getDateFromNow(e.createDate, "date")
        }
      })
      _billFareList.splice(delIndex, 1)

      this.setData({
        billFareList: _billFareList
      })
    })
  },

  indicatorClick(e) {
    console.log(e)
    this.data.currentIndex = e.detail.current + 1
    this.setData({
      currentIndex: this.data.currentIndex
    })
  },
  /**
   * 预览图片/视频
   */
  previewMedia: function () {
    let _billFareMeta = this.data.billFare.billFareMeta
    let imageSoure = _billFareMeta.images.map(e => {
      return {
        url: e,
        type: "image"
      }
    })
    // let videoSoure = _billFareMeta.video.map(e=>{

    // })    
    // debugger
    wx.previewMedia({
      sources: [...imageSoure],
      current: this.data.currentIndex - 1
    })
  },

  /**
   * 附近店铺/店铺详情
   */
  toShopDetail: function (e) {
    wx.navigateTo({
      url: `/pages/shop/shopDetail/index?shopId=${e.detail.id}`,
    })
  },
  selectProduct(e) {
    wx.navigateTo({
      url: `./details?id=${e.detail.id}`,
    })
  }
})